package com.irdstudio.tdp.executor.core.plugin.apps;

import com.irdstudio.tdp.executor.core.plugin.AbstractPlugin;
import com.irdstudio.tdp.executor.core.plugin.gencode.dao.PaasAppsInfoDao;
import com.irdstudio.tdp.executor.core.plugin.gencode.vo.PaasAppsInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.InvocationOutputHandler;
import org.apache.maven.shared.invoker.InvocationRequest;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.Invoker;
import org.apache.maven.shared.invoker.InvokerLogger;
import org.apache.maven.shared.invoker.MavenInvocationException;
import org.apache.maven.shared.invoker.PrintStreamLogger;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;

/**
 * 调用Maven构建插件
 *
 * @author ligm
 * @date 2019-10-14
 */
public class AppMavenBuildPlugin extends AbstractPlugin {

    /**
     * 应用信息
     */
    private PaasAppsInfo appInfo = null;

    /**
     * 读取插件所需要参数
     */
    @Override
    protected boolean doReadConfigureFromDB(Connection conn, String szConfIdentify) throws SQLException {
        // 根据批次流水号读取应用信息
        String batchSerialNo = context.getSzBatchSn();
        PaasAppsInfoDao paasAppsInfoDao = new PaasAppsInfoDao(conn);
        this.appInfo = paasAppsInfoDao.queryByBatchSerialNo(batchSerialNo);
        return true;
    }

    @Override
    public boolean execute() {

        logger.info("调用Maven编译插件，编译应用："+ appInfo.getAppCode());

        // 调用Maven命令构建工程
//		String cmdStr = "cmd /c cd d:\\appproject\\" + appInfo.getAppCode()
//				+ " && mvn clean install -Dmaven.test.skip=true";
        String projectPomPath = SdEnvUtil.PROJECT_PATH + appInfo.getAppCode() + File.separator + "pom.xml";
        logger.info("pom文件：" + projectPomPath);
        InvocationRequest request = new DefaultInvocationRequest();
        if (StringUtils.isNotBlank(SdEnvUtil.MAVEN_SETTINGS_PATH)) {
            request.setUserSettingsFile(new File(SdEnvUtil.MAVEN_SETTINGS_PATH));
        }
        request.setPomFile(new File(projectPomPath));
        request.setGoals(Arrays.asList("clean", "install"));

        Invoker invoker = new DefaultInvoker();
        invoker.setMavenHome(new File(SdEnvUtil.MAVEN_PATH));

        invoker.setLogger(new PrintStreamLogger(System.err, InvokerLogger.WARN) {

        });

        invoker.setOutputHandler(new InvocationOutputHandler() {
            @Override
            public void consumeLine(String s) throws IOException {
                System.out.println(s);
            }
        });

        boolean flag = true;
        InvocationResult result = null;
        try {
            result = invoker.execute(request);
            flag = true;
        } catch (MavenInvocationException e) {
            e.printStackTrace();
            flag = false;
        }

        if (flag) {
            if (result.getExitCode() == 0) {
                logger.info("maven plugin build success");
                flag = true;
            } else {
                logger.info("maven plugin build error");
                flag = false;
            }
        }
        invoker = null;


        /*PaasAppsInfoService paasAppsInfoService = PaasServiceUtil.getPaasAppsInfoService();
        appInfo.setAppActionState("BLD");
        paasAppsInfoService.updateByPk(appInfo);*/
        return flag;
    }

}
